import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import db from '@/utils/localstorage';

export interface Menu {
  alwaysShow: boolean
  children?: Menu[]
  component: string
  hidden: boolean
  meta: { title: string, icon: string, noCache: boolean, link: any }
  name: string
  path: string
  redirect: string
}

interface MenuState {
  list: Menu[]
  active: string[]
}

export const MenuSlice = createSlice({
  name: 'menu',

  initialState: <MenuState>{
    list: db.get('menu', []),
    active: db.get('active', [])
  },

  reducers: {

    init(state: MenuState, action: PayloadAction<Menu[]>) {
      db.set('menu', action.payload)
      state.list = action.payload
    },

    setActive(state: MenuState, action: PayloadAction<string[]>) {
      db.set('active', action.payload)
      state.active = action.payload
    }
  },
})

export default MenuSlice.reducer
